package com.neusoft.controller;

import com.neusoft.core.Food;
import com.neusoft.feign.FoodFeignClient;
import com.neusoft.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import com.neusoft.core.CommonResult;
import com.neusoft.core.Business;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RefreshScope
@RestController
@RequestMapping("/BusinessController")
public class BusinessController {

    @Autowired
    private BusinessService businessService;
    @Autowired
    private FoodFeignClient foodFeignClient;

    @GetMapping("/listBusinessByOrderTypeId/{orderTypeId}")
    public CommonResult<List<Business>> listBusinessByOrderTypeId(@PathVariable("orderTypeId") Integer
                                                                orderTypeId) throws Exception {
        List<Business> list = businessService.listBusinessByOrderTypeId(orderTypeId);
        return new CommonResult<>(200, "success", list);
    }

    @GetMapping("/getBusinessById/{businessId}")
    public CommonResult<Business> getBusinessById(@PathVariable("businessId") Integer
                                                          businessId) throws Exception {

        Business business = businessService.getBusinessById(businessId);
        //在商家微服务中调用食品微服务
        CommonResult<List<Food>> result =foodFeignClient.listFoodByBusinessId(new Food(businessId));
//        System.out.println(result.getMessage());
        if (result.getCode() == 200) {
            business.setFoodList(result.getResult());
        } else {
            business.setFoodList(new ArrayList<Food>());
        }
        return new CommonResult(200, "success", business);
    }

    @RequestMapping("/BusinessesByKeyWord")
    public CommonResult<List<Business>> listBusinessByKeyWord(@RequestParam String keyWord) throws Exception{
        List<Business> list = businessService.listBusinessByKeyWord(keyWord);
        return new CommonResult<List<Business>>(200, "success", list);
    }

    @PostMapping("/BusinessRegistration")
    public CommonResult<Integer> saveBusiness(@RequestParam String userId,
                                              @RequestParam String password,
                                              @RequestParam String businessName) throws Exception {
        int result=businessService.saveBusiness(userId,password,businessName);
        return new CommonResult<>(200, "success", result);
    }

    @PutMapping("/BusinessUpdate")
    public CommonResult<Integer> updateBusiness(@RequestBody Business business) throws Exception{
        int result= businessService.updateBusiness(business);
        return new CommonResult<>(200, "success", result);
    }

    @PostMapping("/BusinessImgUpdate")
    public CommonResult<Integer> updateBusinessImg(@RequestBody Map<String, String> params) throws Exception {
        String businessImg = params.get("businessImg");
        String businessId = params.get("businessId");
        int result= businessService.updateBusinessImg(businessImg, businessId);
        return new CommonResult<>(200, "success", result);
    }
}
